package xin.marcher.module.construction.strategy.status;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.stereotype.Service;
import xin.marcher.module.common.enums.DelFlagEnum;
import xin.marcher.module.common.enums.construction.ProductExceptionCode;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.construction.converter.ProductConverter;
import xin.marcher.module.construction.domain.entity.FrontCategoryRelationDO;
import xin.marcher.module.construction.domain.entity.ItemInfoDO;
import xin.marcher.module.construction.domain.entity.ProductDetailDO;
import xin.marcher.module.construction.domain.request.ProductStatusRequest;
import xin.marcher.module.construction.repository.ProductStatusRepository;
import xin.marcher.module.construction.strategy.route.AbstractStatusStrategy;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 预下架状态变更
 */
@Service
public class AdvanceShelvesStatusService extends AbstractStatusStrategy {

    @Resource
    private ProductStatusRepository productStatusRepository;

    @Resource
    private ProductConverter productConverter;

    /**
     * 上架状态变更到预下架
     *
     * @param productStatusRequest
     */
    @Override
    public void updateProductStatus(ProductStatusRequest productStatusRequest) {
        //1.验证商品和前台类目绑定关系,绑定关系有效
        checkFrontCategoryList(productStatusRequest.getItemId());
        //2. 验证商品数据是否完整
        ProductDetailDO productDetailDO = checkProductInfo(productStatusRequest.getItemId());
        //3.变更商品信息
        updateProduct(productDetailDO, productStatusRequest);

    }

    /**
     * 变更商品的状态
     *
     * @param productDetailDO
     * @param productStatusRequest
     */
    private void updateProduct(ProductDetailDO productDetailDO, ProductStatusRequest productStatusRequest) {
        ItemInfoDO itemInfoDO = productConverter.converterDO(productDetailDO);

        productStatusRepository.updateProductStatus(itemInfoDO, productStatusRequest.getItemStatus());
    }

    /**
     * 验证商品的数据完整性,查询商品
     *
     * @param itemId
     */
    private ProductDetailDO checkProductInfo(String itemId) {
        // 查询商品的详情信息
        ProductDetailDO productDetailDO = productStatusRepository.queryProductDetail(itemId);
        // 验证商品完整性
        productStatusRepository.checkProductComplete(productDetailDO);
        return productDetailDO;
    }

    /**
     * 查询商品是否和前台类目存在绑定关系
     *
     * @param itemId
     */
    private void checkFrontCategoryList(String itemId) {
        // 获取类目绑定关系
        List<FrontCategoryRelationDO> frontCategoryRelationList = productStatusRepository.queryFrontCategoryList(itemId);

        //过滤状态，只保留有效的
        List<FrontCategoryRelationDO> categoryRelationDOList = frontCategoryRelationList.stream().filter(frontCategoryRelationDO -> {
            if (frontCategoryRelationDO.getDelFlag().equals(DelFlagEnum.EFFECTIVE.getCode())) {
                return true;
            }
            return false;
        }).collect(Collectors.toList());
        // 商品不存在有效的绑定关系，提示错误
        if (CollectionUtils.isEmpty(categoryRelationDOList)) {
            throw new BaseBizException(ProductExceptionCode.PRODUCT_CATEGORY_RELATION_NULL);
        }
    }
}
